package cn.ainit.bj.sanshen;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.ainit.common.ShenReturnObj;
import cn.ainit.sys.menu.SessionCont;


@Controller
@RequestMapping("/SanShenVerify")
public class SanShenController {
	
	
	@Resource
	private SanShenService sanShenService;
	public SanShenService getSanShenService() {
		return sanShenService;
	}
	public void setSanShenService(SanShenService sanShenService) {
		this.sanShenService = sanShenService;
	}

	/*******
	 * 获取当前登录人的登录编码
	 */
	@RequestMapping("/getLoginUid")
	@ResponseBody
	public Object getLoginUid(HttpSession session)throws Exception{
		Object uid = session.getAttribute(SessionCont.uid);
		return uid; 
	}
	
	/***
	 * 三审的审核结果  0：待审核 1：审核通过 2：审核未通过  3：驳回上一级  4一审被驳回
	 * 根据书稿三审的Id得到各个级别的审核情况
	 * 返回当前单子要审批的级别  和审批结果，和当前登录人是否具备审批权限 1：是 0：否
	 * @return
	 */
	@RequestMapping("/getVerifyGradeById")
	@ResponseBody
	public ShenReturnObj getVerifyGradeById(String shId,HttpServletRequest request,HttpSession session) {
		ShenReturnObj  shenReturnObj=new ShenReturnObj();
		try {
			//1.定义一审二审三审在对照信息中设置的权限的值
			String gradeRightS1="1";
			String gradeRightS2="2";
			String gradeRightS3="3";

			//2.审批结果值的设置
			String verifyPass="S001";  //审批通过
			String verifyNoPass="S002";  //审批不通过
			String verifyRefuse="S003";
			String verifyReject ="S004";  //一审批被驳回

			//3.获取当前登录人是具有三审的权限
			List verifyGrade=null;
			Object uid = session.getAttribute(SessionCont.uid);
			if(uid == null || "".equals(uid.toString())){
				shenReturnObj.setMsg("fail");
				shenReturnObj.setInfo("用户没有登录，请重新登陆");
				return shenReturnObj;
			}else{
				//判断用户具备三审的级别吗，是属于那个级别的审核
				verifyGrade=sanShenService.shenGradeByUid(uid.toString(),"");
			}

			//4.获取本单据三审的值
			List verifys=sanShenService.getVerifyGradeById(shId,"");
			String s1Verify="";
			String s2Verify="";
			String s3Verify="";
			if(verifys!=null&&verifys.size()>0){
				Map  verifyMaps=(Map)verifys.get(0);
				//一审结果的值
				s1Verify=(String)verifyMaps.get("S1_VERIFY");
				//二审结果的值
				s2Verify=(String)verifyMaps.get("S2_VERIFY");
				//三审结果的值
				s3Verify=(String)verifyMaps.get("S3_VERIFY");
			}

			//1.先判断一审结果
			//一审未进行审批
			if(s1Verify==null||"".equals(s1Verify.trim())){  
				//审核级别设置成1
				shenReturnObj.setShenGrade(1);
				//判断当前登录人是否具备一审的权限
				boolean isShenRight=isShenRight(verifyGrade,gradeRightS1);
				shenReturnObj.setIsShenRight(isShenRight==true?1:0);
				//待审批的时候审批结果设置为0
				shenReturnObj.setShenResult(0);
			}
			//一审审批未通过
			else if(s1Verify.equals(verifyNoPass.trim())){  
				//审核级别设置成1
				shenReturnObj.setShenGrade(1);
				//判断当前登录人是否具备一审的权限
				boolean isShenRight=isShenRight(verifyGrade,gradeRightS1);
				shenReturnObj.setIsShenRight(isShenRight==true?1:0);
				//审批不通过的时候审批结果设置为2
				shenReturnObj.setShenResult(2);
			}
			//一审被驳回
			else if(s1Verify.equals(verifyReject.toString())){
				//审核级别设置成1
				shenReturnObj.setShenGrade(1);
				//判断当前登录人是否具备一审的权限
				boolean isShenRight=isShenRight(verifyGrade,gradeRightS1);
				shenReturnObj.setIsShenRight(isShenRight==true?1:0);
				//当审批被驳回那么一审结果为4
				shenReturnObj.setShenResult(4);
			}

			//2.先判断二审结果
			//二审未进行审批
			else if(s2Verify==null||"".equals(s2Verify.trim())){  
				//审核级别设置成1
				shenReturnObj.setShenGrade(2);
				//判断当前登录人是否具备二审的权限
				boolean isShenRight=isShenRight(verifyGrade,gradeRightS2);
				shenReturnObj.setIsShenRight(isShenRight==true?1:0);
				//待审批的时候审批结果设置为0
				shenReturnObj.setShenResult(0);
			}
			
			//二审审批未通过
			else if(s2Verify.equals(verifyNoPass.trim())){  
				//审核级别设置成2
				shenReturnObj.setShenGrade(2);
				//判断当前登录人是否具备二审的权限
				boolean isShenRight=isShenRight(verifyGrade,gradeRightS2);
				shenReturnObj.setIsShenRight(isShenRight==true?1:0);
				//当审核未通过的时候2
				shenReturnObj.setShenResult(2);
			}
			
			//2.先判断三审结果
			//三审未进行审批
			else if(s3Verify==null||"".equals(s3Verify.trim())){  
				//审核级别设置成3
				shenReturnObj.setShenGrade(3);
				//判断当前登录人是否具备三审的权限
				boolean isShenRight=isShenRight(verifyGrade,gradeRightS3);
				shenReturnObj.setIsShenRight(isShenRight==true?1:0);
				//待审批的时候审批结果设置为0
				shenReturnObj.setShenResult(0);
			}
			//三审审批通过
			else if(s3Verify.equals(verifyPass.trim())){  
				//审核级别设置成3
				shenReturnObj.setShenGrade(3);
				//判断当前登录人是否具备三审的权限
				boolean isShenRight=isShenRight(verifyGrade,gradeRightS3);
				shenReturnObj.setIsShenRight(isShenRight==true?1:0);
				//当审核未通过的时候2
				shenReturnObj.setShenResult(1);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return shenReturnObj;
	}


	public boolean isShenRight(List<Map> verifyGrade,String gradeRight) throws Exception{
		for (int i = 0; i < verifyGrade.size(); i++) {
			String grade=(String)verifyGrade.get(i).get("RIGHT_CODE");
			if(grade.equals(gradeRight)){
				return true;
			}
		}
		return false;
	}

	
	/***
	 * 判断传过来的审核级别判断当前登录人有没有审核该级别的权限
	 * @param grade
	 * @param request
	 * @param session
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/judgeIsShenRight")
	@ResponseBody
	public ShenReturnObj judgeIsShenRight(String grade,HttpServletRequest request,HttpSession session) throws Exception{
		ShenReturnObj shenReturnObj=new ShenReturnObj();
		shenReturnObj.setOwnRight(false);
		try {
			//审核级别grade: 1:一审  2：二审  3：三审
			//审核级别对应的各级的权限  1:一审权限  2:二审权限  4：三审权限
			List<Map> verifyGrade=null;
			Object uid = session.getAttribute(SessionCont.uid);
			if(uid == null || "".equals(uid.toString())){
				shenReturnObj.setMsg("fail");
				shenReturnObj.setInfo("用户没有登录，请重新登陆");
				return shenReturnObj;
			}else{
				//判断用户具备三审的级别吗，是属于那个级别的审核
				verifyGrade=sanShenService.shenGradeByUid(uid.toString(),"");
				if(verifyGrade==null||verifyGrade.size()==0){
					shenReturnObj.setOwnRight(false);
				}
				if(grade.trim().equals("1")){   //一审
					shenReturnObj.setOwnRight(isShenRight(verifyGrade,"1"));
				}else if(grade.trim().equals("2")){//二审
					shenReturnObj.setOwnRight(isShenRight(verifyGrade,"2"));
				}else if(grade.trim().equals("3")){  //三审
					shenReturnObj.setOwnRight(isShenRight(verifyGrade,"3"));
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return shenReturnObj;
	}

	/*******
	 *
	 * @param shenResult
	 * @param shId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/checkShenSave")
	@ResponseBody
	public boolean  checkShenSave (String shenGrade,String shenResult,String shId,HttpSession session) throws Exception{
		//1.定义一审二审三审在对照信息中设置的权限的值
		String gradeRightS1="1";
		String gradeRightS2="2";
		String gradeRightS3="3";

		//2.审批结果值的设置
		String verifyPass="S001";  //审批通过
		String verifyNoPass="S002";  //审批不通过
		String verifyRefuse="S003";  //审批被驳回

		if(shenResult.equals(verifyNoPass)||shenResult.equals(verifyRefuse)){
			return true;
		}
		//前台审核通过
		if(shenResult.equals(verifyPass)){
			//根据shId主键，和shenGrade当前操作的审核等级得到当前审核等级的审核结果
			Map verifys=(Map)sanShenService.getVerifyGradeById(shId,"").get(0);
			String s1Verify=(String)verifys.get("S1_VERIFY");
			String s2Verify=(String)verifys.get("S2_VERIFY");
			String s3Verify=(String)verifys.get("S3_VERIFY");
			//当前操作等级为一审
			if(shenGrade.equals("1")&&shenResult.equals(verifyNoPass)&&s1Verify.equals(shenResult)){
				return false;
			}
			//当前操作等级为二审
			else if(shenGrade.equals("2")&&shenResult.equals(verifyNoPass)&&s2Verify.equals(shenResult)){
				return false;
			}
			//当前操作等级为三审
			else if(shenGrade.equals("3")&&shenResult.equals(verifyNoPass)&&s3Verify.equals(shenResult)){
				return false;
			}
		}
		return false;
	}


	/******
	 * 三审完成之后把审核的Is_verify字段设置成1
	 * @param shId
	 * @param session
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/upIsVerify")
	@ResponseBody
	public boolean upIsVerify(String shId,String verifyVal,String param1,HttpSession session) throws Exception{
		boolean flag=false;
		try {
			String verifyPass="S001";  //审批通过
			String verifyNoPass="S002";  //审批不通过
			String verifyRefuse="S003";  //审批被驳回
			if(verifyVal.trim().equals(verifyPass)){   //如果三级审批通过那么把Is_verify刷成审核通过
				Integer count=sanShenService.upIsVerify(shId);
				flag=count==1?true:false;
			}else{
				return flag;
			}
		} catch (Exception e) {
		}
		return flag;
	}

	
	
	/***************************
	 * 根据审核的结果得到审核结果的编码
	 * 三审的审核结果  0：待审核 1：审核通过 2：审核未通过  3：驳回上一级  4.一审被驳回
	 * 三审结果对应的编码     S001：审批通过    S002：审批不通过    S003:驳回上一级(只有二审和三审有驳回上一级)   S004:一审批被驳回
	 */
	@RequestMapping("/getShenStatue")
	@ResponseBody
	public String getShenStatue(Integer shenStatus,HttpServletRequest request){
		String verifyCode="";
		switch (shenStatus) {
		case 1:
			verifyCode="S001";
			break;
		case 2:
			verifyCode="S002";
			break;
		case 3:
			verifyCode="S003";
			break;
		case 4:
			verifyCode="S004";
			break;
		default:
			break;
		}
		return verifyCode;
	}
	


}
